package com.cskaoyan.order.biz.handler;

import com.cskaoyan.order.biz.context.CreateOrderContext;
import com.cskaoyan.order.biz.context.TransHandlerContext;
import com.cskaoyan.order.mq.DelayOrderCancelProducer;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @Description: 利用mq发送延迟取消订单消息
 **/
@Component
@Slf4j
public class SendMessageHandler extends AbstractTransHandler {

	@Autowired
	DelayOrderCancelProducer orderCancelProducer;

	@Override
	public boolean isAsync() {
		return false;
	}

	@Override
	public boolean handle(TransHandlerContext context) {
		// TODO：讲完消息队列才能做
		// 针对下单成功的一个订单发送一条延迟消息(延迟时间即订单的超时时间)
		//  消息中需要包含，orderId

		// 1. 获取下单成功的订单对应orderId
		CreateOrderContext createOrderContext = (CreateOrderContext) context;
		// 获取到订单ID
		String orderId = createOrderContext.getOrderId();

		// 将订单Id放入延迟消息并发送

		return  orderCancelProducer.sendDelayOrderMessage(orderId);
	}
}